/*
 * Copyright (C) 2016 The Fosafer Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fosafer.idcard_demo.util;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.SparseArray;

import java.io.IOException;


/**
 * Create By :   IDEA
 * Package:      [com.fosafer.idcard_demo.util]
 * ClassName:    [MediaUtil]
 * Description:  [播放音频工具类]
 * Author:       [jianzhic]
 * CreateDate:   [16-4-12 15:05]
 * UpdateUser:   [jianzhic]
 * UpdateDate:   [16-5-12 15:05]
 * UpdateRemark: [Update Description]
 * Version:      [v1.0.0]
 * <p>
 * Copyright (c) 2016 Fosafer Co.,Ltd. All rights reserved.
 */
public class MediaUtil {
    private SparseArray sparseArray = new SparseArray();
    private SoundPool soundPool;

    public MediaUtil(Context context, String fileName1) {
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        try {
            AssetFileDescriptor stopDescriptor = context.getResources().getAssets().openFd(fileName1);
            sparseArray.put(1, soundPool.load(stopDescriptor, 1));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 播放结束音.
     */
    public void playStopSound() {
        soundPool.play((Integer) sparseArray.get(1), 1.0f, 1.0f, 1, 0, 1.0f);
    }

    public void release() {
        if (soundPool != null) {
            soundPool.release();
            soundPool = null;
        }
    }


}
